package model;

import java.util.ArrayList;

import org.bushe.swing.event.EventBus;

import constants.Events;

public class Battle {

    private ArrayList<Question> questions;

    // Perhaps let 'Settings' take care of this
    private int difficulty;

    public Battle() {
        setDifficulty(0);
        questions = new ArrayList<Question>();
        intializeArray(difficulty);
    } //construcor

    /**
     * Sets the difficulty of the questions.
     * @param diff How difficult the questions should be; sets between 0-2
     */
    public final void setDifficulty(final int diff){
        this.difficulty = diff;
    } //setDifficulty

    /**
     * Get the current difficulty.
     * @return battle difficulty level
     */
    public final int getDifficulty(){
        return difficulty;
    } //getDifficulty

    /**
     * Picks a question at random.
     * @return Question Object
     */
    public final Question randQuestion() {
        int rand = (int) (Math.floor(Math.random() * (questions.size())));
        return questions.get(rand);
    } //getQuestion

    /**
     * Set newQuestion into the set of questions.
     * @param str The new question
     * @param answer The answer to the question
     */
    public final void setNewQuestion(final String str, final double answer) {
        Question q = new Question(str, answer);
        questions.add(q);
    }

    Question activeQ;
    public final Question getQuestion() {
        return activeQ;
    } //getQuestion

    //-------------------------------------//
    // Perhaps set this code into the view //
    //-------------------------------------//

    /**
     * Asks the Player a question.
     */
    public final void askPlayer(){

        activeQ = randQuestion();
        String str = activeQ.getQuestion();
        EventBus.publish(Events.BATTLE.toString(), str);
    } //askPlayer


    //------------------------------------//



    /*
     * Dummy method to intialize the array with some
     */
    private void intializeArray(final int diff){
        if (diff == 0){
            setNewQuestion("1+2 = ? ", 3);
            setNewQuestion("6+9 = ? ", 15);
            setNewQuestion("7*2 = ? ", 14);
        }
        else if (diff == 1){
            setNewQuestion("7*2 = ? ", 14);
        }
        else if (diff == 2){
            setNewQuestion("cos(2/5) + sin (2/5) = ?", 1.3104793363115357);
            setNewQuestion("convert 142 to base 2 = ?", 10001110);
        }
    }
}
